package com.itheima.dao.impl;

import com.itheima.dao.AdminDao;
import com.itheima.domain.Admin;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AdminDaoImpl implements AdminDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public Admin findById(Integer primaryId) {
        String sql = "select * from admin where id = ?";
        List<Admin> lists = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class), primaryId);
        return lists.isEmpty() ? null : lists.get(0);

    }

    @Override
    public Admin findByUsername(String username) {
        String sql = "select * from admin where username = ?";
        List<Admin> lists = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class), username);
        return lists.isEmpty() ? null : lists.get(0);
    }

    @Override
    public int updateAdmin(Admin admin) {
        String sql = "update admin set username = ?, address = ?, money = ? where id = ?";
        return jdbcTemplate.update(
                sql,
                admin.getUsername(),
                admin.getAddress(),
                admin.getMoney(),
                admin.getId()
        );
    }
}
